package persist

import (
	"log"
	"gopkg.in/olivere/elastic.v5"
	"context"
	"go-reptile/crawler/engine"
	"github.com/pkg/errors"
)

func ItemSaver(index string) (chan engine.Item,error){
	client , err := elastic.NewClient(
		elastic.SetURL("http://111.231.204.48:9200"),
		elastic.SetSniff(false),
	)
	if err != nil {
		return nil,err
	}

	out :=make(chan engine.Item)
	go func() {
			itemCount := 0
			for {
				item := <- out
				log.Printf("Item Saver: got item #%d: %v",itemCount,item)
				itemCount ++

				// todo:save to elastic search
				err := Save(client,index,item)
				if err != nil {
					log.Printf("item saver: error saving item %v:%v",item,err)
				}

			}
	}()
	return out,nil
}

func Save(client *elastic.Client,index string,item engine.Item)( error){

	if item.Type == "" {
		return  errors.New("must supply Type")
	}

	indexService := client.Index().Index(index).Type(item.Type).
		BodyJson(item)
	if item.Id != "" {
		indexService.Id(item.Id)
	}
	_, e := indexService.Do(context.Background())
	if e != nil {
		return e
	}
	return nil
}
